package com.example.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.example.train.business.domain.Train_Station;
import com.example.train.business.domain.Train_StationExample;
import com.example.train.business.mapper.Train_StationMapper;
import com.example.train.business.req.Train_StationQueryReq;
import com.example.train.business.req.Train_StationSaveReq;
import com.example.train.business.resp.Train_StationQueryResp;
import com.example.train.common.exception.BusinessException;
import com.example.train.common.exception.BusinessExceptionEnum;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Train_StationService {
    private static final Logger LOG = LoggerFactory.getLogger(Train_StationService.class);

    @Resource
    private Train_StationMapper train_StationMapper;

    /**
     * 乘客注册请求的服务类
     * @param req
     */
    public void save(Train_StationSaveReq req) {
        DateTime now = DateTime.now();
        Train_Station train_Station = BeanUtil.copyProperties(req, Train_Station.class);
        if(ObjectUtil.isNull(train_Station.getId())){

            //校验唯一性
            Train_Station trainStationDB = selectByUnique(req.getTrainCode(), req.getIndex());
            if (ObjectUtil.isNotEmpty(trainStationDB)) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
            }
            // 保存之前，先校验唯一键是否存在
            trainStationDB = selectByUnique(req.getTrainCode(), req.getName());
            if (ObjectUtil.isNotEmpty(trainStationDB)) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
            }

            train_Station.setId(SnowUtil.getSnowflakeNextId());
            train_Station.setCreateTime(now);
            train_Station.setUpdateTime(now);
            train_StationMapper.insert(train_Station);
        }else{
            train_Station.setUpdateTime(now);
            train_StationMapper.updateByPrimaryKey(train_Station);
        }
    }

    public List<Train_Station> selectByTrainCode(String trainCode) {
        Train_StationExample trainStationExample = new Train_StationExample();
        trainStationExample.setOrderByClause("`index` asc");
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return train_StationMapper.selectByExample(trainStationExample);
    }

    private Train_Station selectByUnique( String trainCode, Integer index) {
        Train_StationExample trainStationExample = new Train_StationExample();
        trainStationExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andIndexEqualTo(index);
        List<Train_Station> list = train_StationMapper.selectByExample(trainStationExample);
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }

    }
    private Train_Station selectByUnique( String trainCode, String name) {
        Train_StationExample trainStationExample = new Train_StationExample();
        trainStationExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andNameEqualTo(name);
        List<Train_Station> list = train_StationMapper.selectByExample(trainStationExample);
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }

    }

    public PageResp<Train_StationQueryResp> queryList(Train_StationQueryReq req) {
        Train_StationExample train_StationExample = new Train_StationExample();
        train_StationExample.setOrderByClause("train_code asc");
        Train_StationExample.Criteria criteria = train_StationExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        LOG.info("查询页码是 {}", req.getPage());
        LOG.info("每页条数是 {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Train_Station> train_StationList = train_StationMapper.selectByExample(train_StationExample);

        PageInfo<Train_Station> pageInfo = new PageInfo<>(train_StationList);
        LOG.info("总行数是 {}", pageInfo.getTotal());
        LOG.info("总页数是 {}", pageInfo.getPages());

        List<Train_StationQueryResp> list = BeanUtil.copyToList(train_StationList, Train_StationQueryResp.class);

        PageResp<Train_StationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 删除乘客信息的服务实现
     * @param id
     */
    public void delete(Long id) {
        train_StationMapper.deleteByPrimaryKey(id);
    }


}
